<?php
/**
 * MySQL socket
 *
 * @version 0.2a
 */
 
// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" ); 

// Loading Dependencies
coreutil( "cmdl" ) or kill( "cmdl$" );
coreutil( "env" )  or kill( "env$" );

function mysqli() { 
	
	$argv = func_get_args(); $argc = count( $argv );
	
	if ( !is_object( $argv[0] ) )
		$cmdl = cmdl( $argv[0] );
	
	$dbid = ( is_resource( $argv[1] ) ) ? $argv[1] : $argv[2];
	$dbid = ( is_resource( $dbid ) )    ? $dbid    : env("db.mysql.o"); 
	
	$args = ( is_string( $argv[1] ) 
	       && preg_match( "/^\-/", $argv[1] ) ) ? $argv[1] : $argv[2];
	       
	$args = ( is_string( $args )
	       && preg_match( "/^\-/", $args ) )    ? $args    : null;
	
		if ( is_string( $args ) ) eval( ARGL );
	
	/* Connect to Database Host and select DB */
	if ( ( $host = $cmdl["host"] ) 
	  && ( $uid  = $cmdl["u"] ) ) {
		
		$passwd  = $cmdl["p"]; $dbname  = $cmdl["db"];
		
		$dbid    = @mysql_connect( $host, $uid, $passwd, true );
		
		if ( !$dbid ) return false;
		
		if ( $dbname )
			mysql_select_db( $dbname, $dbid );

		if ( !env("db.mysql") )
		      env("db.mysql", array( "o"  => $dbid
		                           , "db" => $dbname ) );
			
		return $dbid;			
		
	/* Select specified database */
	} elseif ( $dbname = $cmdl["db"] ) {
		
	    env( "db.mysql.db", $dbname );
	    
	    return mysql_select_db( $dbname, $dbid );
	
	/* Close connection to MySQL host */
	} elseif ( $exit = $cmdl["exit"] ) {
		
		
		if ( env("db.mysql.o") == $dbid )
		    env("db.mysql", null );
		    
		return mysql_close( $dbid ); 
	
	/* Receive SQL and return results */
	} else {
	
		if ( $s ) return __MYSQL__( $argv[0] );
	
		$sql   = mysql_query( __MYSQL__( $argv[0] ), $dbid );
		
		/** For SQL error, return -1 information */
		if ( mysql_error( $dbid ) ) return -1;
		
		/* SQL returns a BOOL */
        if ( is_bool( $sql ) ) {
	        return $sql;
		} else {
			$fetch = ( $o ) ? "mysql_fetch_object" : "mysql_fetch_assoc";
			
			while( $dataset[] = $fetch( $sql ) ); $sqlc = count( $dataset );
			
			/* Get Resultset and process */
			if ( $dataset[ $sqlc - 1 ] == null ) {
				unset( $dataset[ $sqlc - 1 ] );   $sqlc--; }
			
			if ( !count( $dataset ) ) return;
				
			if ( ( $sqlc == 1 ) && !$l ) {    
		        $dataset = $dataset[0];
				
				if ( count( $dataset ) == 1 ) {
                    
                    if ( $fetch == "mysql_fetch_assoc" ) {
                        $k       = array_keys( $dataset );
                        $dataset = $dataset[ $k[0] ];
                    }
			    }
			}
			
			/* Be responsible, freeing some bits of memory */
			mysql_free_result( $sql );
			
			return $dataset;
		}
	}
}

/* Support for SQL based on SET THEORY */
function __MYSQL__( $sqlo ) {
	
	$o = $sqlo;
	
	if ( is_object( $o ) ) {
	    
	    /* SELECT statement */
		if     ( $o->type == "SELECT" ) {
			
			if ( is_array( $o->fields ) )
			    $o->fields = mysql_real_escape_string( join( ", ", $o->fields ) );
		  
			$sql = "SELECT $o->fields FROM $o->table" 
				 . ( ( $o->where   ) ? " WHERE $o->where"      : "" ) 
				 . ( ( $o->limit   ) ? " LIMIT $o->limit"      : "" )
				 . ( ( $o->groupby ) ? " GROUP BY $o->groupby" : "" )
				 . ( ( $o->order   ) ? " ORDER BY $o->order"   : "" );
		
		/* INSERT statement */
		} elseif ( $o->type == "INSERT" ) {
		
			foreach ( $o->values as $x )
				$v .= "'" . mysql_real_escape_string( $x ) . "', ";
				
			$o->values = substr( $v, 0, strlen( $v ) - 2 );
			$o->fields = join( ", ", $o->fields );
			
			$sql = "INSERT INTO $o->table ( $o->fields ) VALUES ( $o->values )";
		
		/* UPDATE statement */
		} elseif ( $o->type == "UPDATE" ) {
		
			foreach( $o->attribs as $k => $v )
				$args .= "$k = '" . mysql_real_escape_string( $v ) . "', ";
				
			$o->values = substr( $args, 0, strlen( $args ) - 2 ); 
			
			$sql = "UPDATE $o->table SET $o->values"
				 . ( ( $o->where ) ? " WHERE $o->where"      : "" );
		
		/* DELETE statement */
		} elseif ( $o->type == "DELETE" ) {
		
			$sql = "DELETE FROM $o->table"
				 . ( ( $o->where ) ? " WHERE $o->where" : "" );
		
		}
		
		return $sql;
	} else
		return $o;
}
